package components;

import java.util.ArrayList;
import java.util.List;

public class SecurityParser {

   private List<Badword> badwords;
   private List<String> except;
   
   
 /**
 * Constructor for this class.
 *
 */
public SecurityParser() {
    super();
    this.badwords = new ArrayList<Badword>();
    this.except = new ArrayList<String>(); //Excepts als Regex!
    
    this.badwords.add(new Badword("Class", "Badword <i>Class</i> found"));
    this.badwords.add(new Badword("System.exit", "Badword <i>System.exit</i> found"));
    this.badwords.add(new Badword("Runtime", "Badword <i>Runtime</i> found"));
    this.badwords.add(new Badword("Process", "Badword <i>Process</i> found"));
    this.badwords.add(new Badword("Method", "Badword <i>Method</i> found"));
    this.badwords.add(new Badword("Thread", "Badword <i>Thread</i> found"));
    this.badwords.add(new Badword("import", "Badword <i>import</i> found"));
    this.badwords.add(new Badword("java.", "Badword <i>java</i> found"));
    this.badwords.add(new Badword("javax.", "Badword <i>javax</i> found"));
    this.badwords.add(new Badword("Proxy", "Badword <i>Proxy</i> found"));
    this.badwords.add(new Badword("Modifier", "Badword <i>Modifier</i> found"));
    //this.badwords.add(new Badword("Field", "Badword <i>Field</i> found"));
    this.badwords.add(new Badword("Constructor", "Badword <i>Constructor</i> found"));
    this.badwords.add(new Badword("Array", "Badword <i>Array</i> found"));
    this.badwords.add(new Badword("AccessibleObject", "Badword <i>AccessibleObject</i> found"));
    this.badwords.add(new Badword("Member", "Badword <i>Member</i> found"));
    this.badwords.add(new Badword("InvocationHandler", "Badword <i>InvocationHandler</i> found"));
    
    
    this.except.add(" *import +java.awt.Point");
    this.except.add(" *import +de.javakicker.Player");
    this.except.add(" *import +de.javakicker.SoccerAction");
    
}



 /**
 * Returns the except value.
 *
 * @return the except
 */
public List<String> getExcept() {
    return except;
}



/**
 * Sets the except value.
 *
 * @param except the except to set
 */
public void setExcept(List<String> except) {
    this.except = except;
}



/**
 * Returns the badwords value.
 *
 * @return the badwords
 */
public List<Badword> getBadwords() {
    return badwords;
}





/**
 * Sets the badwords value.
 *
 * @param badwords the badwords to set
 */
public void setBadwords(List<Badword> badwords) {
    this.badwords = badwords;
}





    public String checkCode(String code)
    {
        String lines[] = code.split("\n");
        String returnValue = "";
        
        for (int i = 0; i < lines.length; i++) {
            
            for (String tmp: except)
            {
                lines[i] = lines[i].replaceAll(tmp, "");
            }
            
            for (Badword tmp: badwords)
            {
            	
            	int before = lines[i].length();
            	lines[i] = lines[i].replace(tmp.getRegex(), "");
            	int after = lines[i].length();
            	
            	
                if(before!=after)
                {
                	returnValue += tmp.getMessage() + " | on Line " + i + "\n";
                }
            }
        }
        
        return returnValue;
    }
    
}
